home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / os-prober < prev    next >
Text File  |  2009-09-21  |  4KB  |  144 lines

  1. #!/bin/sh
  2. set -e
  3.  
  4. . /usr/share/os-prober/common.sh
  5.  
  6. newns "$@"
  7. require_tmpdir
  8.  
  9. log_output () {
  10.     if type log-output >/dev/null 2>&1; then
  11.         log-output -t os-prober --pass-stdout $@
  12.     else
  13.         $@
  14.     fi
  15. }
  16.  
  17. on_sataraid () {
  18.     type dmraid >/dev/null 2>&1 || return 1
  19.     local parent=${1%/*}
  20.     local device=/dev/${parent##*/}
  21.     if dmraid -r -c | grep -q $device; then
  22.         return 0
  23.     fi
  24.     return 1
  25. }
  26.  
  27. partitions () {
  28.     # Exclude partitions that have whole_disk sysfs attribute set.
  29.     if [ -d /sys/block ]; then
  30.         # Exclude partitions on physical disks that are part of a
  31.         # Serial ATA RAID disk.
  32.         for part in /sys/block/*/*[0-9]; do
  33.             if [ -f "$part/start" ] && \
  34.                [ ! -f "$part/whole_disk" ] && ! on_sataraid $part; then
  35.                 name="$(echo "${part##*/}" | sed 's,[!.],/,g')"
  36.                 if [ -e "/dev/$name" ]; then
  37.                     echo "/dev/$name"
  38.                 fi
  39.             fi
  40.         done
  41.  
  42.         # Add Serial ATA RAID devices
  43.         if type dmraid >/dev/null 2>&1 && \
  44.            dmraid -s -c >/dev/null 2>&1; then
  45.             for raidset in $(dmraid -sa -c); do
  46.                 for part in /dev/mapper/$raidset*[0-9]; do
  47.                     echo "$part"
  48.                 done
  49.             done
  50.         fi
  51.     else
  52.         echo "Cannot find list of partitions!" >&2
  53.         exit 1
  54.     fi
  55.  
  56.     # Also detect OSes on LVM volumes (assumes LVM is active)
  57.     if type lvs >/dev/null 2>&1; then
  58.         echo "$(log_output lvs --noheadings --separator : -o vg_name,lv_name |
  59.             sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|")"
  60.     fi
  61. }
  62.  
  63. parse_proc_mdstat () {
  64.     if type udevadm >/dev/null 2>&1; then
  65.         udevinfo () {
  66.             udevadm info "$@"
  67.         }
  68.     fi
  69.     while read line; do
  70.         for word in $line; do
  71.             dev="${word%%[*}"
  72.             # TODO: factor this out to something in di-utils if
  73.             # it's needed elsewhere
  74.             if [ -d /sys/block ] && type udevinfo >/dev/null 2>&1; then
  75.                 if ! udevinfo -q path -n "/dev/$dev" 2>/dev/null | \
  76.                      grep -q '/.*/.*/'; then
  77.                     continue
  78.                 fi
  79.             elif ! echo "$dev" | grep -q "/part"; then
  80.                 continue
  81.             fi
  82.             raidpart="/dev/$dev"
  83.             echo "$(mapdevfs $raidpart)"
  84.         done
  85.     done
  86. }
  87.  
  88. # Needed for idempotency
  89. rm -f /var/lib/os-prober/labels
  90.  
  91. for prog in /usr/lib/os-probes/init/*; do
  92.     if [ -x $prog ] && [ -f $prog ]; then
  93.         $prog || true
  94.     fi
  95. done
  96.  
  97. # We need to properly canonicalize partitions with mount points and partitions
  98. # used in RAID
  99. grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
  100. : >"$OS_PROBER_TMP/raided-map"
  101. if [ -f /proc/mdstat ] ; then
  102.     grep "^md" /proc/mdstat | parse_proc_mdstat >"$OS_PROBER_TMP/raided-map" || true
  103. fi
  104.  
  105. for partition in $(partitions); do
  106.     if ! mapped=$(mapdevfs $partition); then
  107.         log "Device '$partition' does not exist; skipping"
  108.         continue
  109.     fi
  110.  
  111.     # Skip partitions used in software RAID arrays
  112.     if grep -q "^$mapped" "$OS_PROBER_TMP/raided-map" ; then
  113.         debug "$partition: part of software raid array"
  114.         continue
  115.     fi
  116.  
  117.     if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
  118.         for test in /usr/lib/os-probes/*; do
  119.             if [ -f $test ] && [ -x $test ]; then
  120.                 debug "running $test on $partition"
  121.                 if $test "$partition"; then
  122.                     debug "os detected by $test"
  123.                        break
  124.                 fi
  125.             fi
  126.         done
  127.     else
  128.         mpoint=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)
  129.         mpoint="$(unescape_mount "$mpoint")"
  130.         if [ "$mpoint" != "/target/boot" ] && [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
  131.             type=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 3)
  132.             for test in /usr/lib/os-probes/mounted/*; do
  133.                 if [ -f $test ] && [ -x $test ]; then
  134.                     debug "running $test on mounted $partition"
  135.                     if $test "$partition" "$mpoint" "$type"; then
  136.                         debug "os detected by $test"
  137.                         break
  138.                     fi
  139.                 fi
  140.             done
  141.         fi
  142.     fi
  143. done
  144.